<?php
/**
 * Notes:
 * User: Administrator
 * Date: 2020/3/8
 * Time: 10:09
 * ${PARAM_DOC}
 * @return ${TYPE_HINT}
 * ${THROWS_DOC}
 */

namespace app\api\model;


use think\Db;
use think\Log;
use think\Model;
use app\api\model\Category as CategoryModel;

class Skill extends Model
{
    protected $name = 'skill';

    /**
     * 添加技能
     */
    public static function addRecord($addData = [])
    {
        $data = [
            'user_id' => $addData['user_id'],
            'skilljson' => $addData['skilljson'],
            'createtime' => time(),
            'updatetime' => time()
        ];

        return self::create($data);
    }

    /**
     * 获取用户的第一个岗位
     */
    public static function getUserFirst($uid)
    {
        $skillNames = '';
        $record = self::where(['user_id' => $uid])->order('id')->find();
        if ($record) {
            if ($record['skilljson']) {
                $record['skilljson'] = json_decode($record['skilljson'], true);
                if ($record['skilljson'] && $record['skilljson'][0]) {
                    if (is_array($record['skilljson'][0])) {    // 获取第一个技能
                        //获取第一个技能的第一层的值的key，也就是岗位，值是技能
                        $categoryId = key($record['skilljson'][0]);
                        if ($categoryId) {
                            //$category = CategoryModel::findInfoByCondition(['id' => $categoryId]);
                            $category = Db::name('category')->where(['id' => $categoryId])->find();
                            $skillNames = $category ? $category['name'] : '';
                        }
                    }
                }
            }
        }
        return $skillNames;
    }

    /**
     * 获取技能信息
     */
    public static function findInfoByCondition($condition = [])
    {
        return self::where($condition)->find();
    }

    public static function saveData($id, $data)
    {
        if ($id && $data) {
            $data['updatetime'] = time();
            return self::where(['id' => $id])->update($data);
        }
    }

}